package com.chat.db.nosql.redis.single;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisConfig {
    //为了兼容多个单机redis 不能使用静态


    //jedis单机
    private JedisPool jedisPool;

    //服务器地址
    private String host = "localhost";

    //服务器端口号
    private int port = 6379;

    //AUTH-PASSWORD 默认test
    private String password = "test";

    //可连接最大实例数 默认8
    private int macActive = 8;

    //控制一个pool最大有多少个空闲（idle）的jedis连接 默认8
    private int maxIdle = 8;

    //等待可用连接的最大时间,单位毫秒,默认为-1,表示永不超时,超时则抛出JedisConnectionException
    private int maxWait = 10000;

    //连接超时时间
    private int timeout = 10000;

    //再borrow一个jedis实例时,是否进入volidate操作,如果为true,则得到的jedis实例均为可用的
    private boolean testOnBorrow = true;

    public JedisConfig() {
    }

    public JedisConfig(String host, int port, String password
            , int macActive, int maxIdle, int maxWait
            , int timeout, boolean testOnBorrow) {
        this.host = host;
        this.port = port;
        this.password = password;
        this.macActive = macActive;
        this.maxIdle = maxIdle;
        this.maxWait = maxWait;
        this.timeout = timeout;
        this.testOnBorrow = testOnBorrow;
    }

    public JedisConfig(String host, int port, String password) {
        this.host = host;
        this.port = port;
        this.password = password;
    }

    //在外层的Component上先设置参数/然后再初始化此类/否则会出现参数不存在异常
    //连接方法
    private void buildJedisPool() {
        buildJedisPool(host, port, password, macActive
                , maxIdle, maxWait, timeout, testOnBorrow);
    }

    private void buildJedisPool(String address, Integer port, String password) {
        buildJedisPool(address, port, password, macActive
                , maxIdle, maxWait, timeout, testOnBorrow);
    }

    private synchronized void buildJedisPool(String address, Integer port, String password
            , Integer active, Integer idle, Integer maxWait, Integer timeout
            , boolean testOnBorrow) {
        if (jedisPool == null) {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(active);
            jedisPoolConfig.setMaxIdle(idle);
            jedisPoolConfig.setMaxWaitMillis(maxWait);
            jedisPoolConfig.setTestOnBorrow(testOnBorrow);
            jedisPool = new JedisPool(jedisPoolConfig, address, port, timeout, password);
        }
    }


    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getMacActive() {
        return macActive;
    }

    public void setMacActive(int macActive) {
        this.macActive = macActive;
    }

    public int getMaxIdle() {
        return maxIdle;
    }

    public void setMaxIdle(int maxIdle) {
        this.maxIdle = maxIdle;
    }

    public int getMaxWait() {
        return maxWait;
    }

    public void setMaxWait(int maxWait) {
        this.maxWait = maxWait;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public boolean isTestOnBorrow() {
        return testOnBorrow;
    }

    public void setTestOnBorrow(boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }

    //获取jedis对象
    public Jedis getJedis() {
        //需要在注入之前就设置属性值(@PostStruct)
        if (jedisPool == null) {
            buildJedisPool();
        }
        return jedisPool.getResource();
    }

    public static void main(String[] args) {
        //
        JedisConfig config = new JedisConfig();
        config.buildJedisPool();
        config.getJedis();
    }
}
